#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <net/if.h>

struct ethtool_value {
    __uint32_t cmd;
    __uint32_t data;
};




int main(int argc, char* argv[])
{
    struct ethtool_value edata;
    int fd = -1;
    int ret = 0;
    struct ifreq ifr;

    memset(&ifr, 0, sizeof(struct ifreq));
    strcpy(ifr.ifr_name, argv[1]?argv[1]:"eth0");
    fd = socket(AF_INET, SOCK_DGRAM, 0);
    if(fd < 0){
        fprintf(stderr, "socket error.\n");
        exit(1);
    }

    edata.cmd = 0x0000000a;
    ifr.ifr_data = (caddr_t)&edata;
    ret = ioctl(fd, 0x8946, &ifr);
    if(0 == ret) {
        printf("Link detected: %s\n", edata.data?"yes":"no");
    }
    else if(errno != EOPNOTSUPP) {
        perror("Cannot get link status.\n");
    }

    return 0;
}


